package com.qcxy.gyz.consts;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.HttpHostConnectException;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

public class MNDFitness {

    public static String       INNER_URL          = "http://101.200.165.181";
    public static final int    RESULT_CODE        = 1;
    public static final int    RESULT_CODE_NOINFO = -1;
    public static final int    RESULT_CODE_NOUSER = 0;

    public static final String DEVICE_NO          = "mac";

    public static JSONObject getQRCode(String mac) {
        List<NameValuePair> playerList = new ArrayList<>();
        playerList.add(new BasicNameValuePair(DEVICE_NO, mac));
        // 进行网络请求
        return doPost(INNER_URL + "/api/wcht/getQRCode", playerList, null, 1);
    }
 
    public static JSONObject scanLogin(String mac) {
        List<NameValuePair> playerList = new ArrayList<>();
        playerList.add(new BasicNameValuePair(DEVICE_NO, String.valueOf(mac)));
        // 进行网络请求
        return doPost(INNER_URL + "/api/wcht/scanLogin", playerList, null, 1);
    }

    /**
     * 业务接口：获取版本信息
     */
    public static JSONObject getVersionInfo() {
        List<NameValuePair> paramList = new ArrayList<>();
        return doPost(INNER_URL + "/api/wcht/getNewVersion", paramList, null, 1);
    }
    
    /**
     * 业务接口：上传运动数据数据
     */
    public static JSONObject uploadTrainningData(String openid, String mac,String device_type, JSONArray trainningData) {
    	List<NameValuePair> list = new ArrayList<>();
    	list.add(new BasicNameValuePair("mac", mac));
    	list.add(new BasicNameValuePair("openid", openid));
    	list.add(new BasicNameValuePair("device_type", device_type));
    	list.add(new BasicNameValuePair("trainningData", trainningData.toString()));
    	return doPost(INNER_URL + "/api/wcht/upLoadExerciseData", list, null, 1);
    }

    private static JSONObject doPost(String url, List<NameValuePair> paramList, JSONObject jsonData, int flag) {
        HttpPost post = new HttpPost(url);
        HttpEntity entity = null;
        try {
            switch (flag) {
                case 1:
                    entity = new UrlEncodedFormEntity(paramList, HTTP.UTF_8);
                    break;
                case 2:
                    entity = new StringEntity(jsonData.toString());
                    break;
                default:
                    break;
            }

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        post.setEntity(entity);
        try {
            DefaultHttpClient defaultHttpClient = new DefaultHttpClient();
            defaultHttpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000);// 连接时间
            defaultHttpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 60000);// 数据传输时间
            HttpResponse r = defaultHttpClient.execute(post);
            if (r != null && r.getStatusLine().getStatusCode() == 200) {
                String entityStr = EntityUtils.toString(r.getEntity(), "UTF-8");
                if (entityStr.startsWith("{")) {
                    try {
                        return new JSONObject(entityStr);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        } catch (HttpHostConnectException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
